#!/bin/bash

# Slackware build script for mksh

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Original author: Markus Reichelt, Aachen, DE
# Now maintained by B. Watson <urchlay@slackware.uk>

# 20230102 bkw: BUILD=2
# - take over maintenance.
# - relicense as WTFPL.
# - install FAQ.htm, not mksh.faq.
# - make the test suite optional (but default to enabled).
# - simplify the build script.
# - make the slack-desc show the build options.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=mksh
VERSION=${VERSION:-R59c}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
   i?86) ARCH=i586 ;;
   arm*) ARCH=arm ;;
      *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

CC="gcc"
SLKCFLAGS="${SLKCFLAGS} -Wall"

# 20230102 bkw: fail if user passes both DIET=yes and MUSL=yes.
if [ "$DIET" = "yes" -a "$MUSL" = "yes" ]; then
  echo "$( basename $0 ): cannot combine DIET=yes with MUSL=yes." 1>&2
  exit 1
fi

if [ "$DIET" = "yes" ]; then
  # no need to log out & back in after installing dietlibc.
  source /etc/profile.d/dietlibc.sh
  CC="diet -Os gcc"
  # diet builds are always static
  STATIC=yes
  BUILDOPTS+="DIET=yes "
elif [ "$MUSL" = "yes" ]; then
  CC="musl-gcc"
  BUILDOPTS+="MUSL=yes "
fi

if [ "$STATIC" = "yes" ]; then
  LDFLAGS="${LDFLAGS} -static"
  CPPFLAGS="${CPPFLAGS} -DMKSH_NOPWNAM"
  BUILDOPTS+="STATIC=yes "
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM

chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

LDFLAGS="$LDFLAGS" \
CPPFLAGS="$CPPFLAGS" \
CFLAGS="$SLKCFLAGS" \
CC="$CC" \
  sh Build.sh -r

# make sure tests pass whether there is a controlling tty or not. There are
# some changes related to this in CVS, so this can probably be removed with the
# next release.
# thanks alpine maintainers.
# 20230102 bkw: make tests optional with TESTS=no. This is a maintainer-mode
# option (saves me time if I'm repeatedly editing/running the script).
if [ "${TESTS:-yes}" = "yes" ]; then
  sed -i -e '/^name: selftest-tty-absent$/,/^---$/d' check.t
  ./test.sh -C regress:no-ctty
fi

# 20230102 bkw: turns mksh.faq into proper HTML (FAQ.htm).
sh FAQ2HTML.sh

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC $PKG/{bin,usr/man/man1,etc/skel,install}

install -m 755 -s mksh       $PKG/bin
install -m 644    dot.mkshrc $PKG/etc/skel/.mkshrc.new
install -m 644    dot.mkshrc $PKGDOC
install -m 644    FAQ.htm    $PKGDOC

for i in mksh lksh; do
  gzip -9c < $i.1 > $PKG/usr/man/man1/$i.1.gz
done

cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

cat $CWD/slack-desc > $PKG/install/slack-desc
[ -n "$BUILDOPTS" ] && \
  sed -i "15s,\$, Build options: $BUILDOPTS," $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
